package com.yiren.dbaa.module.practice_set.map.presenter;

import android.app.Activity;

import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.backstage.currency.entity.LineInfoEntity;
import com.yiren.backstage.currency.entity.MapEntity;
import com.yiren.backstage.db.dao.controller.UserDbController;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.practice_set.line.contract.LineHomeContract;
import com.yiren.dbaa.module.practice_set.line.model.LineHomeModel;
import com.yiren.dbaa.module.practice_set.map.contract.MapManageContract;
import com.yiren.dbaa.module.practice_set.map.model.MapManageModel;
import com.yiren.dbaa.view.toast.MessageBox;

import java.util.List;

/**
 * @author lwb
 * @date : 2022/11/29 17:17
 * @description: 地图管理
 */
public class MapManagePresenter implements MapManageContract.Presenter {

    private Activity activity;
    private MapManageContract.Model mapManageModel;
    private LineHomeContract.Model lineHomeModel;
    private UserParamConfigBean userParamConfig;

    public MapManagePresenter(Activity activity) {
        this.activity = activity;
        mapManageModel = new MapManageModel(activity);
        lineHomeModel = new LineHomeModel(activity);
        userParamConfig = LoadConfigApi.loadUserParamConfig(activity);
    }

    @Override
    public void changeUserRange(MapManageContract.View view, MapEntity map, int range) {
        mapManageModel.updateUserRange(userParamConfig.getUserInfo().getId().toString(), map, range, new CommonCallBack() {
            @Override
            public void success(Object result) {
                //本地修改
                mapManageModel.updateUserRangeLoacl(map, range);
                activity.runOnUiThread(() -> view.updateUseRange(range));
                MessageBox.show(activity, "更改成功!");
            }

            @Override
            public void fail(String error) {
                MessageBox.show(activity, error);
            }
        });
    }

    @Override
    public void getMapBindLines(String mapId, MapManageContract.View view) {
        mapManageModel.getMapBindLines(mapId, new CommonCallBack() {
            @Override
            public void success(Object result) {
                view.setMapBindLines((List<LineInfoEntity>) result);
            }

            @Override
            public void fail(String error) {
                MessageBox.show(activity, error);
            }
        });
    }

    @Override
    public void getLineList(String userId, String siteCode, CommonCallBack callBack) {
        lineHomeModel.getLineFromServer(userId, siteCode, false, callBack);
    }

    @Override
    public void bindLinesToMap(MapEntity map) {
        UserDbController.getInstance(false).update(map);
    }

}